﻿using System;
using System.Collections.Generic;
using System.Text;
using TerminationCall.Core;
using System.Drawing;
using TerminationCall.Editor.Logic;

namespace TerminationCall.Editor.GUI {
    public class Renderer {

        const int ResolutionX = 480;
        const int ResolutionY = 272;

        private EditorTool _editorTool;

        public bool DrawGrid { get; set; }

        public Renderer(EditorTool editorTool) {
            _editorTool = editorTool;

            DrawGrid = false;
        }

        public Bitmap RenderLevel(Point Offset) {

            Bitmap bitmap = new Bitmap(ResolutionX, ResolutionY);
            Graphics graphics = Graphics.FromImage(bitmap);

            graphics.DrawImage(_editorTool.Level.Background, Point.Empty);

            for (int x = Offset.X; x < Offset.X + 25; x++) {
                for (int y = 0; y < 17; y++) {
                    if (_editorTool.Level.Layers[0].Fields[x, y].BackgroundTile != null) {
                        graphics.DrawImage(_editorTool.Level.Layers[0].Fields[x, y].BackgroundTile.Bitmap, (x - Offset.X) * TileConstants.TileSizeX, y * TileConstants.TileSizeY);
                    }

                    Tile[] tiles = _editorTool.Level.Layers[0].Fields[x, y].TileField.Tiles;

                    foreach (Tile tile in tiles) {
                        graphics.DrawImage(tile.Bitmap, (x - Offset.X) * TileConstants.TileSizeX, y * TileConstants.TileSizeY);
                    }

                    if (_editorTool.Level.Layers[0].Fields[x, y].ForegroundTile != null) {
                        graphics.DrawImage(_editorTool.Level.Layers[0].Fields[x, y].ForegroundTile.Bitmap, (x - Offset.X) * TileConstants.TileSizeX, y * TileConstants.TileSizeY);
                    }
                }
            }

            if (DrawGrid) {
                for (int x = 0; x < 25; x++) {
                    graphics.DrawLine(new Pen(Color.FromArgb(50, 0, 0, 0)), x * TileConstants.TileSizeX, 0, x * TileConstants.TileSizeX, 272);
                }
                for (int y = 0; y < 18; y++) {
                    graphics.DrawLine(new Pen(Color.FromArgb(50, 0, 0, 0)), 0, y * TileConstants.TileSizeY, 481, y * TileConstants.TileSizeY);
                }
            }

            return bitmap;
        }

        public Bitmap RenderSelection(TileSet tileSet, Rectangle selection) {

            if (tileSet == null) {
                return new Bitmap(1, 1);
            }

            if (selection.IsEmpty) {
                return new Bitmap(1, 1);
            }

            if (selection.Width <= 0 || selection.Height <= 0) {
                return new Bitmap(1, 1);
            }


            Bitmap bitmap = new Bitmap((selection.Right - selection.Left) * TileConstants.TileSizeX, (selection.Bottom - selection.Top) * TileConstants.TileSizeY);
            Graphics graphics = Graphics.FromImage(bitmap);

            graphics.Clear(Color.Transparent);

            for (int x = selection.Left; x < selection.Right; x++) {
                for (int y = selection.Top; y < selection.Bottom; y++) {

                    graphics.DrawImage(tileSet.Tiles[x, y].Bitmap, (x - selection.Left) * TileConstants.TileSizeX, (y - selection.Top) * TileConstants.TileSizeY);
                }
            }

            return bitmap;

        }
    }
}
